{extend name="layout/base" /}

{block name="style"}
<style type="text/css">
    /*.table>tbody>tr>td{padding: 3px 5px;}*/
</style>
{/block}

{block name="body"}
<div class="wrapper wrapper-content">

<div class="row animated fadeIn">
    <div class="col-xs-12">
        <div class="ibox box-success">
            <div class="ibox-title">
                <h5>用户管理列表</h5>
            </div>
            <div class="panel-body ibox-content mainbox">
                <div class="row">
                    <form action="" method="get">
                    <div class="col-md-2 col-sm-2 pull-right">
                        <div class="input-group">
                            <input type="text" class="form-control" id="c-keyword" name="keyword" value="{$keyword ?? ''}" placeholder="" />
                            <span class="input-group-btn" style="padding:0;border:none;">
                                <button type="submit" class="btn btn-primary btn-search">搜索</button>
                            </span>
                        </div>
                    </div>
                    </form>
                </div>
                <table class="table table-striped table-bordered cs_table-hover m-t-sm">
                    <tr>
                        <th width="60">用户ID</th>
                        <th width="10%">用户名</th>
                        <th width="10%">昵称</th>
                        <th width="8%">备注</th>
                        <th>Email</th>
                        <th>手机号</th>

                        <th>等级</th>
                        <th>积分</th>
                        <th>累计积分</th>
                        <th>游戏绑定</th>
                        <th>注册时间</th>
                        <th><a href="?orderby=activetime">最后活跃时间</a></th>
                        <th>状态</th>

                        <th>操作</th>
                    </tr>
                    {foreach $datas as $v}
                    <tr b-id="{$v['id']}" b-username="{$v['username']}" b-score="{$v['score']}">
                        <td>{$v['id']}</td>
                        <td>{$v['username']}</td>
                        <td>{$v['nickname']}</td>
                        <td><span style="font-size:12px;">{$v['memo']}</span></td>
                        <td>{$v['email']}</td>
                        <td>{$v['mobile']}</td>

                        <td>{$v['level']}</td>
                        <td>{$v['score']}</td>
                        <td>{$v['totalscore']}</td>
                        <td>{$v['gamebinded']}</td>
                        <td>{$v['createtime']}</td>
                        <td>{$v['activetime']|date='Y-m-d H:i:s',###}</td>
                        <td>
                            {if $v['status']!='pending'}
                            <input class="i-checks sw-status" type="checkbox"{if $v['status']=='normal'} checked="checked"{/if} />
                            {/if}
                        </td>
                        <td>
                            <a href="javascript:;" class="viewBtn">查看</a> |
                            <a href="{:Url('/user/edit', ['id'=>$v['id']])}" class="editBtn">修改</a> |
                            <a href="javascript:;" class="scoreBtn">增减积分</a>
                        </td>
                    </tr>
                    {/foreach}
                </table>
                <div class="row">
                    <div class="col-xs-12">{$pager}</div>
                </div>
            </div><!--mainbox-->
        </div>

    </div><!--col-md-->
</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="scoreModal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="" method="post">
                <div class="modal-header">
                    <button type="button" class="close closeModal" aria-hidden="true"> &times;</button>
                    <h4 class="modal-title" id="scoreModalLabel">增减积分</h4>
                </div>
                <div class="modal-body">
                    <table class="table table-striped table-bordered">
                        <tr>
                            <td width="20%">用户名：</td>
                            <td><input class="form-control input-medium" type="text" name="username" /></td>
                        </tr>
                        <tr>
                            <td width="20%">当前积分：</td>
                            <td><input class="form-control input-medium" type="text" name="score" /></td>
                        </tr>
                        <tr>
                            <td width="20%">增减积分：</td>
                            <td><input class="form-control input-medium" type="number" name="change_score" /></td>
                        </tr>
                        <tr>
                            <td width="20%">是否增减<br/>积分经验：</td>
                            <td><input class="i-checks sw-status" type="checkbox" value="true" name="add_scoreexp" checked="checked" /></td>
                        </tr>
                        <tr>
                            <td width="20%">原因：</td>
                            <td><input class="form-control input-medium" type="text" name="remark" /></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <input type="hidden" name="rid" />
                                <input type="button" value="确定" class="btn btn-primary doSubmit"/>
                                <button type="button" class="btn btn-danger closeModal">关闭</button>
                            </td>
                        </tr>
                    </table>
                </div>
            </form>
        </div>
    </div>
</div><!--modal-->

<!-- 模态框（Modal） -->
<div class="modal fade" id="viewModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" style="width: 900px;">
        <div class="modal-content">
            <form action="" method="post">
                <div class="modal-header">
                    <button type="button" class="close closeViewModal" aria-hidden="true"> &times;</button>
                    <h4 class="modal-title" id="viewModalLabel">查看</h4>
                </div>
                <div class="modal-body">
                    <h3>用户信息：</h3>
                    <div>
                        <table class="table table-striped table-bordered tableBase">
                            <tbody>
                            </tbody>
                        </table>
                    </div>

                    <h3>游戏绑定信息：</h3>
                    <table class="table table-striped table-bordered tableGames">
                        <thead>
                        <tr style="text-align: center;">
                            <th>区服名</th>
                            <th>区服ID</th>
                            <th>角色名</th>
                            <th>角色ID</th>
                            <th>角色等级</th>
                            <th>VIP等级</th>
                            <th>VIP经验</th>
                            <th>尊享认证</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </form>
        </div>
    </div>
</div><!--modal-->

</div><!--wrapper-->
{/block}

{block name="script"}
<!--公共JS-->
{include file="common/commjs" /}
<script type="text/javascript">

    $(function () {
        // 复选框按钮样式
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'icheckbox_square-green'
        });

        $('.mainbox .sw-status').on('ifChanged', function () {
            var dataPat = $(this).parents('tr:first');
            var rid = dataPat.attr('b-id');

            var parms = {
                'url': '{:Url("/user/setter")}',
                'data': {}
            };
            if ($(this).is(':checked')) {
                parms['data'] = {'rid': rid, 'field': 'status', 'val': 'normal'};
                sendAjax(parms);
            } else {
                parms['data'] = {'rid': rid, 'field': 'status', 'val': 'hidden'};
                sendAjax(parms);
            }
        });

        $('.mainbox .scoreBtn').on('click', function () {
            var dataPat = $(this).parents('tr:first');
            var rid = dataPat.attr('b-id');
            var username = dataPat.attr('b-username');
            var score = dataPat.attr('b-score');

            $('#scoreModal input[name="rid"]').val(rid);
            $('#scoreModal input[name="username"]').val(username);
            $('#scoreModal input[name="score"]').val(score);

            $('#scoreModal input[name="username"]').attr('disabled', 'disabled');
            $('#scoreModal input[name="score"]').attr('disabled', 'disabled');

            $('#scoreModal').modal('show');
        });

        $('.mainbox .viewBtn').on('click', function () {
            var dataPat = $(this).parents('tr:first');
            var rid = dataPat.attr('b-id');
            var parms ={
                'url': '{:Url("/user/view")}',
                'data': {rid: rid}
            };
            sendAjax(parms).done(function(response, statusText, jqXHR){
                if(!response) {
                    return false;
                }
                var info = response['data'];
                var bindwechat = '';
                var bindqq = '';

                if (info['verification']['wechat'] == 1) {
                    bindwechat = '已绑定';
                }
                if (info['verification']['qq'] == 1) {
                    bindqq = '已绑定';
                }

                var str = '';
                str += '<tr><td>用户名</td><td>'+info['username']+'</td></tr>';
                str += '<tr><td>昵称</td><td>'+info['nickname']+'</td></tr>';
                str += '<tr><td>微信</td><td>'+(info['wechat']!='' ? info['wechat']+'&nbsp;' : '')+bindwechat+'</td></tr>';
                str += '<tr><td>QQ</td><td>'+(info['qq']!='' ? info['qq']+'&nbsp;' : '')+bindqq+'</td></tr>';
                // str += '<tr><td>手机验证</td><td>'+info['verifyemail']+'</td></tr>';
                // str += '<tr><td>邮箱验证</td><td></td></tr>';
                $('#viewModal table.tableBase tbody').html(str);

                var item2 = [];
                var games = response['data']['games'];
                $.each(games, function (i, v) {
                    var str2 = "<tr style='text-align: left;'><td>" +
                        v['server'] + "</td><td>"+
                        v['server_id'] + "</td><td>"+
                        v['name'] + "</td><td>"+
                        v['roleid'] + "</td><td>"+
                        v['role_level'] + "</td><td>"+
                        v['vip_level'] + "</td><td>"+
                        v['vip_exp'] + "</td><td>"+
                        v['iszun'] + "</td></tr>";
                    item2.push(str2);
                });

                $('#viewModal table.tableGames tbody').html(item2.join(''));
                $('#viewModal').modal('show');
            });
            // var url = 'http://www.baidu.com';
            // options = $.extend({
            //     type: 2,
            //     title: '查看',
            //     shadeClose: true,
            //     shade: false,
            //     maxmin: true,
            //     moveOut: true,
            //     area: ['800px', '600px'],
            //     content: url,
            //     success: function (layero, index) {
            //
            //     }
            // });
            // layer.open(options);
        });

        $('#viewModal .closeViewModal').on('click', function () {
            $('#viewModal').modal('hide');
        });

        // 表单重置
        var formReset = function (former) {
            $('#scoreModal input[name="username"]').removeAttr('disabled');
            $('#scoreModal input[name="score"]').removeAttr('disabled');
            former[0].reset(); // former[0]转化成DOM对象
        };

        $('#scoreModal .doSubmit').on('click', function () {
            var former = $(this).parents('form:first');
            var selize = former.serialize();
            var rid = $('#scoreModal input[name="rid"]').val();
            if (rid == 0) {
                alertError('参数错误');
                formReset(former);
                return false;
            } else {
                var parms ={
                    'url': '{:Url("/user/score_change")}',
                    'data': selize
                };
                sendAjax(parms);
            }

        });

        $('#scoreModal .closeModal').on('click', function () {
            var former = $(this).parents('form:first');
            formReset(former);
            $('#scoreModal').modal('hide');
        });

    });
</script>
{/block}